{include file="header.tpl"}
<script type="text/javascript" src="./js/jquery.js"></script>
<script type="text/javascript">
	
$(document).ready(function()
{
  $(".delete").live('click', function(event) 
  {
    $(this).parent().remove();
    setTotAmount();
    setGrossTotal();
    setVatTotal();
    setCstTotal();
  });
});
	
function addNewRow()
{
  $('#billBook tr:last').after('<tr class="billRow"><td><input type="text" name="particulars[]" /></td><td><input type="text" name="hnsCode[]" /></td><td><input type="text" name="grossWeight[]" class="grossWeight" onChange="setAmount(this)" /></td><td><input type="text" name="fineWeight[]"/></td>	<td><input type="text" name="rate[]" class="rate" onChange="setAmount(this)" /></td><td><input type="text" name="amount[]" class="amount" onChange="setAmount(this)" /></td>	<td><a href="javascript:addNewRow();">Add</a></td></td><td class="delete">Remove<\/td></tr>');
}

function setAmount(txtObj)
{
  var row = $(txtObj).parents('.billRow');
  var price = parseFloat(row.find('.grossWeight').val()) * parseFloat(row.find('.rate').val());
  isNaN(price) ? row.find('.amount').val("") : row.find('.amount').val(price);
  setTotAmount();
}

function setTotAmount()
{
	var setTotAmount = 0;
	
	$('.amount').each(function(i)
  {
    price = $(this).val();
    if (!isNaN(price)) setTotAmount += Number(price);
  });
  
  $('#amountTotal').val(setTotAmount);
  setGrossTotal();
  setVatTotal();
  setCstTotal();
}

function setVatTotal()
{
  var vatTotal = (parseFloat(document.form1.amountTotal.value) * parseFloat(document.form1.vat.value))/100;
  document.form1.vatTotal.value = vatTotal;
  setGrossTotal();
}

function setCstTotal()
{
	var cstTotal = (parseFloat(document.form1.amountTotal.value) * parseFloat(document.form1.cst.value))/100;
	document.form1.cstTotal.value = cstTotal;
	setGrossTotal();
}

function setGrossTotal()
{
	var amountTotal = isNaN(parseFloat(document.form1.amountTotal.value)) ? 0 : parseFloat(document.form1.amountTotal.value);
	var vatTotal = isNaN(parseFloat(document.form1.vatTotal.value)) ? 0 : parseFloat(document.form1.vatTotal.value);
	var cstTotal = isNaN(parseFloat(document.form1.cstTotal.value)) ? 0 : parseFloat(document.form1.cstTotal.value);
  var grossTotal = amountTotal + vatTotal + cstTotal;
  document.form1.grossTotal.value = grossTotal;
}

</script>
<form name="form1" action="" method="post">
<input type="hidden" name="billId" value="{$billMst.billMstId}" />
<center>
  <b>Shree Ram Silver</b><br/>
  All Kind Of Fancy Silver Jewllery<br/>
  6-shree Ranchhodnager Socity, Pedak Road<br/>
  Behind Navodit School. Rajkot-3 Mo.- 9898195000
</center><br/>
<select name="invoice">
	<option value="tax">Tax Invoice</option>
	<option value="retail">Retail Invoice</option>
</select>
<table border="1" width="100%">
<tr>
	<td valign="top">
		<div class="bill">
			<label>Ms :</label>
			<input type="text" name="partyName" value="{$billMst.partyName}" /><br/><br/>
			<label>Adress :</label>
			<textarea name="partyAddress">{$billMst.partyAdd}</textarea><br/><br/>
			<label>Party Tin No :</label>
			<input type="text" name="partyTinNo" value="{$billMst.partyTinNo}" />
		</div>
	</td>
	<td valign="top">
		<div class="bill">
			<label>Bill No :</label>
			<input type="text" name="billNo" value="{$billMst.billNo}" /><br/><br/>
			<label>Bill Date :</label>
			{html_select_date prefix="billDate" start_year="-2" end_year="+1" field_order="DMY" day_value_format="%02d" time=$billMst.billDate}
		</div>
	</td>
</tr>
</table>
<br/>
<table id="billBook" width="100%">
<tr class="table1">
	<th>Particulars</th>
	<th>HNS<br/> Code</th>
	<th>Gross<br/> Weight</th>
	<th>Fine<br/> Weight</th>
	<th>Rate</th>
	<th>Amount<br/> Rs.</th>
	<th colspan="2"></th>
</tr>
{section name="sec" loop=$billDetail|@count}
<tr class="billRow">
	<td><input type="text" name="particulars[]" value="{$billDetail[sec].particular}" /></td>
	<td><input type="text" name="hnsCode[]" value="{$billDetail[sec].hnsCode}" /></td>
	<td><input type="text" name="grossWeight[]" class="grossWeight" value="{$billDetail[sec].grossWeight}" onChange="setAmount(this)" /></td>
	<td><input type="text" name="fineWeight[]" value="{$billDetail[sec].fineWeight}" /></td>
	<td><input type="text" name="rate[]" class="rate" onChange="setAmount(this)" value="{$billDetail[sec].rate}" /></td>
	<td><input type="text" name="amount[]" class="amount" onChange="setAmount(this)" value="{$billDetail[sec].amount}" /></td>
	<td><a href="javascript:addNewRow();">Add</a></td>
  <td class="delete">Remove</td>
</tr>
{/section}
<tr class="billRow">
	<td><input type="text" name="particulars[]" /></td>
	<td><input type="text" name="hnsCode[]" /></td>
	<td><input type="text" name="grossWeight[]" class="grossWeight" onChange="setAmount(this)" /></td>
	<td><input type="text" name="fineWeight[]"/></td>
	<td><input type="text" name="rate[]" class="rate" onChange="setAmount(this)" /></td>
	<td><input type="text" name="amount[]" class="amount" onChange="setAmount(this)" /></td>
	<td><a href="javascript:addNewRow();">Add</a></td>
  <td width="50">&nbsp;</td>
</tr>
</table>
<hr/>
<table align="right">
	<tr>
		<th align="right" width="170">Total</th>
		<td width="170"><input type="text" name="amountTotal" id="amountTotal" style="text-align: right;" value="{$billMst.totalAmount}" onChange="setTotAmount();" /></td>
		<td width="80">&nbsp;</td>
	</tr>
	<tr>
		<th align="right" width="170">Vat <input type="text" name="vat" size="1" style="text-align: right;" value="{$billMst.vatInPer}" onChange="setVatTotal()" /> %</th>
		<td width="170"><input type="text" name="vatTotal" style="text-align: right;" value="{$billMst.vatTotal}" onChange="setVatTotal()" /></td>
		<td width="80">&nbsp;</td>
	</tr>
	<tr>
		<th align="right" width="170">C.S.T <input type="text" name="cst" size="1" style="text-align: right;" value="{$billMst.cstInPer}" onChange="setCstTotal()" /> %</th>
		<td width="170"><input type="text" name="cstTotal" style="text-align: right;" style="text-align: right;" value="{$billMst.cstTotal}" onChange="setCstTotal()" /></td>
		<td width="80">&nbsp;</td>
	</tr>
	<tr>
		<th align="right" width="170">G. Total</th>
		<td width="170"><input type="text" name="grossTotal" style="text-align: right;" value="{$billMst.grossTotal}" onChange="setTotAmount();" /></td>
		<td width="80">&nbsp;</td>
	</tr>
</table>
<div style="clear:both"></div>
Notes:<br />
<textarea name="notes" cols="50">{$billMst.notes}</textarea><br />
<input type="submit" name="submitBtn" value="Save"/>
</form>
<script type="text/javascript">
{if $billMst|@count > 0}
  for(var a = 0 ; a < document.form1.invoice.length ; a++)
  {
    if(document.form1.invoice[a].value == "{$billMst.invoice}")
      document.form1.invoice[a].selected = true;
  }	
{/if}
</script>
{include file="footer.tpl"}